package com.chinaway.dsp.action;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.mina.core.session.IoSession;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;
import com.chinaway.dsp.conn.AssemblyPacket;
import com.chinaway.dsp.util.AccStatus;

public class IteratorSendAction implements Runnable{
	private Display display;
	private Text log_info;
	private IoSession session;
	private String totaltime;
	private String iterationtime;
	private String msg_course="";
	private String msg_imei="";
	private String msg_speed="";
	private String msg_lng="";
	private String msg_lat="";
	private int sendCount=1;
	private long currentTimeMillis=System.currentTimeMillis();
	public boolean destroyThread=false;
	private int devicetype;
	private AccStatus accStatus;
	private Button send_iteration_btn;
	private Button acc_status_btn;
	
	public IteratorSendAction(IoSession s,Display d,Text t,String imei,String speed,String course,String lng,String lat,String send_totaltime,String send_iteratortime,int device,AccStatus as,Button send_iteration_btn,Button acc_status_btn){
		this.session=s;
		this.display=d;
		this.log_info=t;
		this.totaltime=send_totaltime;
		this.iterationtime=send_iteratortime;
		this.msg_imei=imei;
		this.msg_course=course;
		this.msg_speed=speed;
		this.msg_lng=lng;
		this.msg_lat=lat;
		this.devicetype=device;
		this.accStatus=as;
		this.send_iteration_btn=send_iteration_btn;
		this.acc_status_btn=acc_status_btn;
	}
	
	@Override
	public void run() {
		while(true){
			if(destroyThread)
				break;
			display.asyncExec(new Runnable() {
				public void run() {
	            	Date currentTime = new Date();
	            	if((currentTime.getTime()-currentTimeMillis)>=(Long.valueOf(totaltime)*1000*60)){
    					log_info.append("发送完毕\n");
    					send_iteration_btn.setText("连续发");
    					acc_status_btn.setText("点火");
    					accStatus.setStatus(0);
    					destroyThread=true;
    					//sendCount=1;
    					return;
    				}
    				String msg_seq=AssemblyPacket.string2wordhex(String.valueOf(sendCount));	
    	   		 	SimpleDateFormat formatter = new SimpleDateFormat("yy MM dd HH mm ss");
    	   		 	String dateString = formatter.format(currentTime);
    	   		 	String msg="";
    	   		 	if(0==devicetype)
    	   		 		msg="7E 05 00 00 27 "+msg_imei+" "+msg_seq+" 1D "+AssemblyPacket.toAcc(accStatus.getStatus())+" "+msg_lat+" "+msg_lng+" 00 00 "+msg_speed+" "+msg_course+" "+dateString+" 00 00 00 00 00 0E 08 30 00 01 BA 53 70 4C 11 C0 B9 7E";
    				if(1==devicetype)
    					msg="7E 02 00 00 1C "+msg_imei+" "+msg_seq+" 00 00 00 00 00 00 "+AssemblyPacket.toAcc(accStatus.getStatus())+" "+msg_lat+" "+msg_lng+" 00 00 "+msg_speed+" "+msg_course+" "+dateString+" B9 7E";
    	   		 	byte[] mes = AssemblyPacket.transfer2Bytes(msg);
    				setNewFlag(mes);
    				byte[] message=AssemblyPacket.edit7d7e(mes);
    				session.write(message);
    				String send=AssemblyPacket.byteToString(message);
    				System.out.println("msg="+send);
    				SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    			 	String date = formatter1.format(currentTime);
    				log_info.append(date+" 发送：\n"+send+"\n");
    				sendCount++;
            	}
			}
	        );
			try {
				Thread.currentThread();
				Thread.sleep(Long.valueOf(iterationtime)*1000);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	private void setNewFlag(byte[] bs) {
		byte[] da = new byte[bs.length - 15];
		System.arraycopy(bs, 13, da, 0, da.length);
		//ctbox校验位
		//bs[bs.length - 4] = setNewFlagSub(da);
		//然后gnns校验位
		int idx = bs.length - 2;  
		bs[idx] = bs[1];
		for (int j=2;j<idx;j++) bs[idx] ^= bs[j];
	}
}
